LoginWindow = Ext.extend(Ext.panel.Panel, {
//    title: "用户登录",
    border: false,
    closable: false,
    draggable: false,
    height: 200,
    layout: 'border',
    initComponent: function () {
        var _this = this;
        Ext.apply(this, {
            items: [
                {
                    region: 'center',
                    itemId: 'form',
                    xtype: 'form',
                    layout: 'form',
                    border: false,
                    bodyStyle: "padding:10px;",
                    labelWidth: 75, // label settings here
                    // cascade unless overridden
                    defaults: {
                        height: 32,
                        labelAlign: 'right',
                        labelSeparator: "："
                    },
                    defaultType: 'textfield',
                    items: [
                        {
                            fieldLabel: '登录邮箱',
                            name: 'username',
                            allowBlank: false,
                            specialkey: function (field, e) {
                                if (e.getKey() == e.ENTER) {
                                    _this.login();
                                }
                            }
                        },
                        {
                            fieldLabel: '登录密码',
                            name: 'password',
                            inputType: 'password',
                            allowBlank: false,
                            specialkey: function (field, e) {
                                if (e.getKey() == e.ENTER) {
                                    _this.login();
                                }
                            }
                        },
                        {
                            xtype: 'container',
                            layout: 'hbox',
                            height: 32,
                            items: [
                                {
                                    flex: 1,
                                    height: 32,
                                    labelAlign: 'right',
                                    xtype: 'textfield',
                                    fieldLabel: '验证码',
                                    name: 'captcha',
                                    labelSeparator: "：",
                                    allowBlank: false,
                                    listeners: {
                                        specialkey: function (field, e) {
                                            // e.HOME, e.END, e.PAGE_UP, e.PAGE_DOWN,
                                            // e.TAB, e.ESC, arrow keys: e.LEFT, e.RIGHT, e.UP, e.DOWN
                                            if (e.getKey() == e.ENTER) {
                                                _this.login();
                                            }
                                        }
                                    }
                                },
                                {
                                    width: 51,
                                    xtype: 'displayfield',
                                    renderer: function (v) {
                                        return '<img id="captcha" src="../captcha.png"  height="30" onclick="this.src = \'../captcha.png?_t = \' +new Date().getTime() "/>';
                                    }
                                }
                            ]
                        },
                        {
                            xtype: 'container',
                            html: '<div class="loginBtn"><a class="button" onclick="login()">立即登录</a> &nbsp;&nbsp;&nbsp;<a href="#" onclick="resetPwdWin();">密码找回</a></div>'
                        }
                    ]
                }
            ]
        });

        if (this.qr) {
            this.items.push({
                border: false,
                region: 'east',
                width: 237,
                bodyStyle: '',
                html: '<div id="qr_img" style="margin: 5px;text-align: center"></div><div style="text-align: center">微信扫一扫</div>'
            });
        }
        LoginWindow.superclass.initComponent.call(this);

        if (this.qr) {
            var appkey = "140226478";
            $.getJSON("../login_qr_url.do", function (json) {
                if (json.qr_url) {
                    var longurl = json.qr_url;
                    longurl = encodeURIComponent(longurl);
                    $.ajax({
                        //使用iPad新浪微博客户端的Appkey
                        url: "http://api.weibo.com/2/short_url/shorten.json?source=" + appkey + "&url_long=" + longurl,
                        type: 'GET',
                        dataType: 'jsonp', //使用JSONP方法进行AJAX
                        cache: false,
                        success: function (data) {
                            if (1 === data.code) {
                                $('#qr_img').html('<img src="../str2qr_img.do?content=' + encodeURIComponent(data.data.urls[0].url_short) + '" style="width:120px;height:120px;" />');
                            } else {
                                $('#qr_img').html('<img src="../str2qr_img.do?content=' + encodeURIComponent(longurl) + '" style="width:120px;height:120px;" />');
                            }
                        }
                    });
                    var checking = false;

                    function checkLogin() {
                        if (checking) {
                            return;
                        }
                        checking = true;
                        $.get('../login_qr.do', function (json) {
                            if (json.success) {
                                location.href = "frame.do";
                            }
                            checking = false;
                        });
                    }

                    setInterval(checkLogin, 1500);
                }
            });
        }
    },
    login: function () {
        this.getComponent('form').getForm().submit({
            clientValidation: true,
            url: 'login.do',
            success: function (form, action) {
                top.location.href = "frame.do";
//                登录成功后不需要再获取新的验证码了
//                document.getElementById('captcha').src = "../captcha.png?_t=" + new Date().getTime();
            },
            failure: ajax_form_failed
        });
    },
    resetPwdWin: function () {

        Ext.create('Admin.ResetPwd.Win', {
            title: '密码找回',
            formUrl: F.url('/account/getPwd/retake.do'),
            submitSuccessCallback: function (win) {
                win.close();
            },
            submitFailureCallback: function (win) {
                UI.showErrorMsg("网络异常，请重试....");
            }
        }).show();
    }
});


/**
 * 找回密码win
 */
Ext.define('Admin.ResetPwd.Win', {
    extend: 'Ext.window.Window',
    height: 150,
    maximizable: false,
    width: 350,
    layout: 'fit',
    resizable: false,
    buttonAlign: "center",
    modal: true,
    initComponent: function () {
        var win = this;
        Ext.apply(this, {
            buttons: [
                {
                    text: "提交",
                    scope: this,
                    handler: function (b, e, t) {
                        var email = this.getComponent('emailForm').getForm().findField('email').getValue();

                        if (!email || email.length < 1) {
                            UI.showErrorMsg("【温馨提示】", "您没有输入Email信息!");
                            return;
                        }
                        b.disable();
                        win.setTitle('邮件发送中...');
                        Ext.Ajax.request({
                            url: win.formUrl,
                            params: {
                                email: email
                            },
                            success: function (response, opts) {
                                b.enable();
                                var obj = Ext.decode(response.responseText);
                                if (obj.success) {
                                    UI.showInfoMsg("【温馨提示】", obj.msg);
                                    win.close();
                                } else {
                                    UI.showErrorMsg("【温馨提示】", obj.error || "请重试...");
                                }


                            },
                            failure: ajax_failed
                        });
                    }
                }, {
                    text: "取消",
                    scope: this,
                    handler: function (o, e, t) {
                        this.getComponent('emailForm').getForm().reset();
                        win.close();
                    }
                }]
        });

        var emailForm = Ext.create('Ext.form.Panel', {
            layout: 'anchor',
            id: 'emailForm',
            url: this.formUrl,
            border: false,
            defaultType: 'textfield',
            margin: "20 20 20 20",
            //bodyStyle:"padding-top:20px",
            defaults: {
                anchor: '100%',
                labelWidth: 60
            },
            items: [{
                fieldLabel: '邮箱地址',
                name: 'email',
                allowBlank: false,
                vtype: 'email',
                emptyText: '请输入邮箱地址',
                blankText: '邮箱地址不能为空...'
            }]
        });

        this.items = [emailForm];
        this.callParent();
    }
});
